const {getAllTypes,createType,deleteTypeById,updateType} = require('../services/typeService')

module.exports = {
    getAllTypes: async(ctx,next) => {
        try {
            let types = await getAllTypes()
            if(types) {
                ctx.body = {
                    success: true,
                    data: {types: types},
                    info: {msg: "获取类别列表成功"},
                    code: 200
                }
            } else {
                ctx.body = {
                    success: false,
                    data: {types: types},
                    info: {msg: "获取类别列表失败"},
                    code: 200
                }
            }
        } catch(error) {
            console.log(error)
            ctx.body = {
                success: false,
                data: {},
                info: {msg: "服务器请求异常"},
                code: 500
            }
        }
    },
    addType: async(ctx,next) => {
        try {
            const {typeName} = ctx.request.body
                const addTypeResult =  await createType(typeName)
                if(addTypeResult) {
                    ctx.body = {
                        success: true,
                        data: 'addTypeResult:addTypeResult',
                        info: {msg: "添加角色信息成功"},
                        code: 200
                    }
                } else {
                    ctx.body = {
                        success: false,
                        data: 'addTypeResult:addTypeResult',
                        info: {msg: "添加角色信息失败"},
                        code: 200
                    }
                }
        } catch(error) {
            console.log(error)
            ctx.body = {
                success: false,
                data: {},
                info: {msg: "服务器请求异常"},
                code: 500
            }
        }
    },
    deleteType: async(ctx,next) => {
        try {
            const id = ctx.params.id
                const deleteTypeResult = await deleteTypeById(id)
                if(deleteTypeResult) {
                    ctx.body = {
                        success: true,
                        data: {deleteTypeResult:deleteTypeResult},
                        info: {msg: "删除类别成功"},
                        code: 200
                    }
                } else {
                    ctx.body = {
                        success: false,
                        data: {},
                        info: {msg: "删除类别失败"},
                        code: 200
                    }
                }       
        } catch(error) {
            console.log(error)
            ctx.body = {
                success: false,
                data: {},
                info: {msg: "服务器请求异常"},
                code: 500
            }
        }
    },
    updateType: async(ctx,next) => {
        try {
            const {id,typeName} = ctx.request.body
                const updateTypeResult =  await updateType(id,typeName)
                if(updateTypeResult) {
                    ctx.body = {
                        success: true,
                        data: {updateTypeResult:updateTypeResult},
                        info: {msg: "更新类别成功"},
                        code: 200
                    }
                } else {
                    ctx.body = {
                        success: false,
                        data: {},
                        info: {msg: "更新类别失败"},
                        code: 200
                    }
                }
        } catch(error) {
            console.log(error)
            ctx.body = {
                success: false,
                data: {},
                info: {msg: "服务器请求异常"},
                code: 500
            }
        }
    }
}